package com.chixing.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.mapper.CouponMapper;
import com.chixing.mapper.MycouponMapper;
import com.chixing.pojo.Coupon;
import com.chixing.pojo.Mycoupon;
import com.chixing.service.IMycouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zht
 * @since 2022-10-26
 */
@Service
public class MycouponServiceImpl extends ServiceImpl<MycouponMapper, Mycoupon> implements IMycouponService {
    @Autowired
    private MycouponMapper mycouponMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Override
    public Integer insert(Mycoupon mycoupon) {
        int insert = mycouponMapper.insert(mycoupon);
        return insert;
    }

    @Override
    public List<Mycoupon> selectAll(Integer userId) {
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Mycoupon> couponList = mycouponMapper.selectList(wrapper);
        return couponList;
    }

    @Override
    public List<Mycoupon> selectAll2Use(Integer userId) {
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Mycoupon> couponList = mycouponMapper.selectList(wrapper);
        for (int i=couponList.size()-1;i>0;i--) {
            Integer couponId = couponList.get(i).getCouponId();
            Coupon coupon = couponMapper.selectById(couponId);
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            LocalDateTime couponEndTime = coupon.getCouponEndTime();
            LocalDateTime now = LocalDateTime.now();
            boolean result = now.isBefore(couponEndTime);
            if(!result)
                couponList.remove(couponList.get(i));
            else if(couponList.get(i).getMycouponStatus()==0)
                couponList.remove(couponList.get(i));
        }
        //System.out.println(couponList);
        return couponList;
    }

    @Override
    public Integer update(Mycoupon mycoupon) {
//        1表示未使用  0表示已使用  2表示已过期
        if(mycoupon.getMycouponStatus()==1){
            mycoupon.setMycouponStatus(0);
            int i = mycouponMapper.updateById(mycoupon);
            return i;
        }else {
            return null;
        }

    }
    @Override
    public Long getCouponNumberByUserId(String userId) {
        //select count from mycoupon where userid = 1
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        Long couponNumber = Long.valueOf(mycouponMapper.selectCount(wrapper));
        System.out.println("优惠券数量：=============="+couponNumber);
        return couponNumber;
    }
    @Override
    public Integer delete(String mycouponNumber) {
        QueryWrapper<Mycoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("mycoupon_number",mycouponNumber);
        int delete = mycouponMapper.delete(wrapper);
        return delete;
    }
}
